package com.exchange.used.config.utils;

import com.google.gson.JsonArray;

import java.awt.Color;
import java.lang.reflect.Field;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.regex.Pattern;

public class FormatUtil {
    private static final Pattern NUMBER;

    static {
        NUMBER = Pattern.compile("-?[0-9]*\\.?[0-9]+");
    }

    /**
     * 转Json数组
     * @param list 列表
     * @param <T> 任意实现过toString方法的对象
     * @return Json数组
     */
    public static <T extends Number> JsonArray number2JsonArray(List<T> list) {
        JsonArray array = new JsonArray();
        for(T e: list) {
            array.add(e);
        }
        return array;
    }

    /**
     * 转Json数组
     * @param list 列表
     * @return Json数组
     */
    public static JsonArray str2JsonArray(List<String> list) {
        JsonArray array = new JsonArray();
        for(String e: list) {
            array.add(e);
        }
        return array;
    }

    /**
     * 获得16进制颜色表示
     *
     * @param color Java awt 颜色
     * @return 16进制颜色
     */
    public static String getColor(Color color) {
        return String.format("#%02x%02x%02x%02x",
                color.getRed(),
                color.getGreen(),
                color.getBlue(),
                color.getAlpha());
    }

    public static String byte2String(byte[] bytes){
        StringBuilder stringBuilder = new StringBuilder();
        if(bytes != null){
            for(byte b : bytes){
                stringBuilder.append((char) b);
            }
            return stringBuilder.toString();
        }else {
            return "";
        }
    }

    /**
     * 将前段传来的值（字符串）转换Entity属性类型
     * @param field 属性实体
     * @param value 值
     * @return 转换后的值
     */
    public static Object getStdReflectValue(Field field, String value) {
        if(field == null || value == null || value.trim().equals("")){
            return null;
        }
        value = value.trim();
        Class<?> type = field.getType();
        if (String.class.equals(type)) {                                        //String
            return value;
        } else if(short.class.equals(type) || Short.class.equals(type)) {       //Short
            return Short.valueOf(value);
        } else if(int.class.equals(type) || Integer.class.equals(type)) {       //Integer
            return Integer.valueOf(value);
        } else if(long.class.equals(type) || Long.class.equals(type)) {         //Long
            return Long.valueOf(value);
        } else if(float.class.equals(type) || Float.class.equals(type)) {       //Float
            return Float.valueOf(value);
        } else if(double.class.equals(type) || Double.class.equals(type)) {     //Double
            return Double.valueOf(value);
        } else if (boolean.class.equals(type) || Boolean.class.equals(type)) {  //Boolean
            return Boolean.valueOf(value);
        } else if (byte[].class.equals(type) || Byte[].class.equals(type)) {    //Byte[]
            return value.getBytes();
        } else if (char.class.equals(type) || Character.class.equals(type)) {   //Character
            return value.charAt(0);
        } else if (char[].class.equals(type) || Character[].class.equals(type)) {   //Character
            return value.toCharArray();
        } else if (Date.class.equals(type)) {                                   //Date
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            try {
                return sdf.parse(value);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        return value;
    }

    /**
     * Entity数据类型转换为字符串
     * @param field 实体类中反射的字段
     * @param value 实体类
     * @return 字符串
     */
    public static String formatValue(Field field, Object value) {
        if(field == null || value == null){
            return "";
        }
        Class<?> fieldType = field.getType();
        if (String.class.equals(fieldType)) {
            return value.toString();
        } else if (boolean.class.equals(fieldType) || Boolean.class.equals(fieldType)) {
            return value.toString();
        } else if (char.class.equals(fieldType) || Character.class.equals(fieldType)) {
            return value.toString();
        } else if (char[].class.equals(fieldType) || Character[].class.equals(fieldType)) {
            return new String((char[])value);
        } else if (byte[].class.equals(fieldType) || Byte[].class.equals(fieldType)) {
            return new String((byte[])value);
        } else if (short.class.equals(fieldType) || Short.class.equals(fieldType)) {
            return value.toString();
        } else if (int.class.equals(fieldType) || Integer.class.equals(fieldType)) {
            return value.toString();
        } else if (long.class.equals(fieldType) || Long.class.equals(fieldType)) {
            return value.toString();
        } else if (float.class.equals(fieldType) || Float.class.equals(fieldType)) {
            return value.toString();
        } else if (double.class.equals(fieldType) || Double.class.equals(fieldType)) {
            return value.toString();
        } else if (Date.class.equals(fieldType)) {
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-DD HH:mm:ss");
            return dateFormat.format(value);
        } else {
            throw new RuntimeException("request illeagal type, type must be Integer not int Long not long etc, type=" + fieldType);
        }
    }


    /**
     * 获得对应类型的全部小写名称构成的Map
     * @param clazz 需要反射的类型
     * @return Map
     */
    public static Map<String, Field> getLowerCaseFields(Class<?> clazz) {
        Map<String, Field> fieldMap = new HashMap<>();
        for(Field field: clazz.getDeclaredFields()) {
            fieldMap.put(field.getName().toLowerCase(), field);
        }
        return fieldMap;
    }

}
